<?php
/**
 * Hoffman Framework
 *
 * LICENSE
 *
 * This source file is subject to the GNU LGPL 2.1 license that is bundled
 * with this package in the file license.txt.
 * It is also available through the world-wide-web at this URL:
 * http://creativecommons.org/licenses/LGPL/2.1
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to annop@thaicyberpoint.com so we can send you a copy immediately.
 *
 * @package    Hoffman
 * @author     Ford AntiTrust
 * @copyright  Copyright (c) 2008 Ford AntiTrust (http://www.thaicyberpoint.com/ford/blog)
 * @since      Version Rv 0.2
 * @license    http://creativecommons.org/licenses/LGPL/2.1
 *             GNU LGPL 2.1
 * @filesource
 */

if (!defined('INDEX'))
    die('No direct script access allowed.');

/**
 * Logs
 *
 * @package     Hoffman
 * @subpackage  Hmf
 * @category    Errors
 * @author      Ford AntiTrust
 * @version     SVN: $Id: Errors.php 158 2009-07-18 05:19:42Z annop.s@gmail.com $
 * @link        http://www.thaicyberpoint.com/ford/blog/hmf
 */

/**
 * @see Zend_Registry
 */
require_once 'Zend/Registry.php';

/**
 * @see Hmf_Logs
 */
require_once 'Hmf/Logs.php';

class Hmf_Errors {

    public static function pageNotFound() {

        $heading = "Page Not Found";
        $message = "The page you requested was not found.";

        self::showError(404, $heading, $message, 'error');
    }

    public static function invalidZFVersion() {

        self::showError('xHmf0', 'Zend Framework Version', 'Your Zend Framework version is older than '. HMF_USED_ZF, 'error');
    }

    public static function routing() {

        self::showError('xHmf1', 'Routing', "The routing error.", 'error');
    }

    public static function missingControllerFile($controllerFile) {

        self::showError('xHmf2', 'Missing the controller file.', "The controller file (<b>{$controllerFile}</b>) does not exist.", 'error');
    }

    public static function missingControllerClass($controllerClass) {

        self::showError('xHmf3', 'Missing the controller class.', "The controller class (<b>{$controllerClass}</b>) does not exist.", 'error');
    }

    public static function missingAction($actionName, $controllerName) {

        self::showError('xHmf4', 'Missing the action.', "The action (<b>{$actionName}</b>) in controller (<b>{$controllerName}</b>) class does not exist.", 'error');
    }

    public static function missingContentpageFile($stickyKey) {

        self::showError('xHmf5', 'Missing the contentpage file.', "The contentpage.<b>{$stickyKey}</b>'s file does not exist.", 'error');
    }

    public static function missingMasterpageFile($masterPageFile) {

        self::showError('xHmf6', 'Missing the masterpage file', "The masterpage's file (<b>{$masterPageFile}</b>) does not exist.", 'error');
    }

    public static function missingArgs($number_missing_args, $name_controller_missing_args, $name_action_missing_args) {

        self::showError('xHmf7', 'Missing the arguments', "Need {$number_missing_args} argument(s) for action <b>{$name_action_missing_args}</b> in controller <b>{$name_controller_missing_args}</b>'.", 'error');
    }

    public static function missingComponent($componentClassName) {

        self::showError('xHmf8', 'Missing the component class.', "The component class (<b>{$componentClassName}</b>) does not exist.", 'error');
    }

    public static function missingConstructor($className) {

        self::showError('xHmf9', 'Missing the constructor method.', "The constructor method (<b>{$className}</b>) does not exist.", 'error');
    }

    public static function missingModel($className) {

        self::showError('xHmf10', 'Missing the model.', "The model (<b>{$className}</b>) does not exist.", 'error');
    }

    public static function classNotExtends($superClassName, $className) {

        self::showError('xHmf11', "Class not extends from  <b>{$superClassName}</b> class.", "The <b>{$className}</b> class not extends from <b>{$superClassName}</b> class.", 'error');
    }

    public static function accessDenied() {

        self::showError('xHmf12', "Access denied", "Access denied.", 'error');
    }

    public static function missingExtendedHelpers($helperFileName) {

        self::showError('xHmf13', 'Missing the extended helpers file.', "The extended helpers file (<b>".PATH_DIR_APPLICATION_HELPERS."/{$helperFileName}.php</b>) does not exist.", 'error');
    }

    public static function showError($errno, $heading, $message, $page) {

        Hmf_Logs::sendErrorToLogs("CODE/ID: " . $errno . "\t" . preg_replace("/<[^>]+>/", "", $heading) . "\t" . preg_replace("/<[^>]+>/", "", $message));

        $message = '<p>' . implode('</p><p>', (!is_array($message)) ? array($message) : $message) . '</p>';

        if (ob_get_level() > ob_get_level() + 1) {
            ob_end_flush();
        }
        if (!Zend_Registry::get('is_production') or $errno == 404) {
            ob_start();
            require_once (PATH_DIR_APPLICATION_VIEWS_ERRORS . '/' . $page . '.php');
            $buffer = ob_get_contents();
            ob_end_clean();
            echo $buffer;
            exit();
        } else {
            self::pageNotFound();
        }

    }
}
?>